/**
 * 
 * Opened Touchable RIA Framework - A Web RIA Javascript Framework
 * 
 * Copyright (c) 2012
 * Dual licensed under the MIT or GPL v2 licenses.
 * 
 * @author  CuTe_CuBe
 * @version $Id: shape.js 6 2012-03-10 12:51:49Z cc.hgfeng@gmail.com $
 */

/**
 * OTR.Graph.Shape
 * 提供绘图
 * 
 * @class OTR.Graph
 * @constructor
 * @param object params
 * 形状通用参数列表
 * width:   绘图宽度
 * height:  绘图高度
 * x:       位置横坐标
 * y:       位置纵坐标
 * borderWidth: 边框大小
 * borderColor: 边框颜色
 * background:  背景
 */
OTR.Graph.Shape = function(params) {
    //通用属性
    this._attrs = {
        x: 0, y: 0, width: 0, height: 0,
        borderWidth: 0,
        borderColor: '#000000',
        background: '#ffffff'
    };
    this._init(params);
};

OTR.Graph.Shape.prototype = {
     
     /**
      * 初始化对象
      * 
      * @param object params
      */
     _init: function(params) {
         this.setAttrs(params);
     },
     
     /**
      * 获取 / 写入属性
      * 
      * @param string name
      * @param mixed  value
      * @return mixed
      */
     attr: function(name, value) {
         if (undefined === value) {
         
             if (typeof (name) == 'object') {
                 this.setAttrs(name);
                 return this;
             }

             return this.getAttr(name);
         } else {
             if (undefined !== this._attrs[name]) {
                 this._attrs[name] = value;
             }
             
             return this;
         }
     },
     
     /**
      * 获取属性值
      * 
      * @param string name
      * @return mixed
      */
     getAttr: function(name) {
         if (undefined !== this._attrs[name]) {
             return this._attrs[name];
         }
         
         return null;
     },

     /**
      * 设置当前对象属性
      * 
      * @param object params
      * @return OTR.Graph.Shape
      */
    setAttrs: function(params) {
        for (var i in params) {
            if (undefined !== this._attrs[i]) {
                this._attrs[i] = params[i];
            }
        }
        
        return this;
    },
    
    /**
     * 获取当前形状名称
     * 
     * @return string
     */
    getName: function() {
        return this.__name;
    },
    
    /**
     * 添加属性
     * 
     * @protected
     * @param string | array names
     */
    _addAttrs: function(names) {
        if (typeof names == 'string') {
            this._attrs[names] = null;
        } else if (OTR.Util.isObject(names)) {
            for (var k in names) {
                this._attrs[k] = names[k];
            }
        }
    },
    
    /**
     * 设置形状标识
     * 
     * @protected
     * @param string
     */
    _setName: function(name) {
        if (!OTR.Util.isString(name)) {
            throw 'name must be a string';
        }
        
        this.__name = name;
    }
};